package com.zenithmind.article.pojo.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 文章标签关联DTO
 * 遵循单一职责原则：专注于文章标签关联数据传输
 * 遵循里氏替换原则：正确继承BaseDTO
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Schema(name = "文章标签DTO", description = "文章标签关联数据传输对象")
public class ArticleTagDTO extends BaseDTO {

    @NotBlank(message = "文章ID不能为空")
    @Schema(description = "文章ID", required = true)
    private String articleId;

    @NotEmpty(message = "标签ID列表不能为空")
    @Schema(description = "标签ID列表", required = true)
    private List<String> tagId;

    @Schema(description = "操作类型：add-添加，remove-移除，replace-替换")
    private String operationType = "add";

    @Override
    protected boolean validateRequired() {
        return articleId != null && !articleId.trim().isEmpty()
               && tagId != null && !tagId.isEmpty();
    }

    @Override
    protected boolean validateBusinessRules() {
        // 验证标签ID列表中没有重复项
        if (tagId != null) {
            long distinctCount = tagId.stream().distinct().count();
            if (distinctCount != tagId.size()) {
                return false; // 存在重复的标签ID
            }
        }
        return true;
    }

    @Override
    public boolean isValid() {
        return validateRequired() && validateBusinessRules();
    }

    @Override
    public String getBusinessKey() {
        return articleId;
    }

    /**
     * 检查是否为添加操作
     */
    public boolean isAddOperation() {
        return "add".equalsIgnoreCase(operationType);
    }

    /**
     * 检查是否为移除操作
     */
    public boolean isRemoveOperation() {
        return "remove".equalsIgnoreCase(operationType);
    }

    /**
     * 检查是否为替换操作
     */
    public boolean isReplaceOperation() {
        return "replace".equalsIgnoreCase(operationType);
    }

    /**
     * 获取标签数量
     */
    public int getTagCount() {
        return tagId != null ? tagId.size() : 0;
    }
}
